package day24_1590;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName Solution
 * @Description TODO
 * @Author clockTown
 * @Date 2021/6/2 18:41
 * @Version 1.0
 */
public class Solution1 {
    public static int solve(int[] nums, int k) {

        long tar = 0;

        for (int n : nums)
            tar += n;

        tar = Math.floorMod(tar, k);

        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, -1);

        long prefix = 0;
        int res = nums.length;

        for (int i = 0; i < nums.length; i++) {

            prefix += nums[i];
            int mod = (int) Math.floorMod(prefix, (long) k);
            map.put(mod, i);

            if (map.containsKey(Math.floorMod(prefix - tar, k)))
                res = Math.min(res, i - map.get(Math.floorMod(prefix - tar, k)));
        }

        return res == nums.length ? -1 : res;
    }


    public static void main(String[] args) {
        int[] nums = {3, 1, 4, 2};
        int p = 6;
        System.out.println(solve(nums, p));

    }
}
